<template>
	<view class="diary-detail-container">
		<view class="diary-card" v-if="diary">
			<!-- 标题区域 -->
			<view class="diary-header">
				<view class="title-section">
					<text class="diary-title">{{ diary.title }}</text>
					<view class="mood-tag">{{ moodMap[diary.mood] || '😊' }}</view>
				</view>

				<view class="date-section">
					<text class="diary-date">{{ formatDate(diary.recordTime) }}</text>
					<text class="diary-time">{{ formatTime(diary.recordTime) }}</text>
				</view>
			</view>

			<!-- 内容区域 -->
			<view class="diary-content">
				<view v-if="diary.textType === 'markdown'" class="markdown-content" v-html="renderedMarkdown"></view>
				<view v-else class="normal-content">{{ diary.content }}</view>
			</view>

			<!-- 图片区域 -->
			<view v-if="diaryImgs.length > 0" class="image-section">
				<view class="section-title">图片</view>
				<view class="image-list">
					<view v-for="(img, index) in diaryImgs" :key="index" class="image-item"
						@click="previewImage(index)">
						<image :src="img" mode="aspectFill" class="diary-image"></image>
					</view>
				</view>
			</view>

			<!-- 操作按钮 -->
			<view class="action-buttons">
				<button class="action-btn edit-btn" @click="editDiary">编辑</button>
				<button class="action-btn delete-btn" @click="deleteDiary">删除</button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed,
		onMounted
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		marked
	} from 'marked'
	import dayjs from 'dayjs'
	import request from '../../../utils/request'

	// 响应式数据
	const diary = ref(null)
	const loading = ref(true)

	// 心情映射
	const moodMap = {
		'happy': '😊',
		'sad': '😢',
		'angry': '😠',
		'excited': '🤩',
		'calm': '😌'
	}

	// 计算属性
	const diaryImgs = computed(() => {
		if (!diary.value || !diary.value.imgs) return []
		return diary.value.imgs.split(',').filter(img => img.trim() !== '')
	})

	const renderedMarkdown = computed(() => {
		if (!diary.value || diary.value.textType !== 'markdown' || !diary.value.content) return ''

		marked.setOptions({
			breaks: true,
			gfm: true
		})

		return marked(diary.value.content)
	})

	// 方法
	const formatDate = (date) => {
		return dayjs(date).format('YYYY年MM月DD日')
	}

	const formatTime = (date) => {
		return dayjs(date).format('HH:mm')
	}

	const loadDiaryDetail = async (id) => {
		try {
			loading.value = true
			const response = await request.get(`/diary/detailById/${id}`)

			if (response.code === 200) {
				diary.value = response.data
			} else {
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				})
			}
		} catch (error) {
			console.error('加载日记详情失败:', error)
			uni.showToast({
				title: '加载失败',
				icon: 'none'
			})
		} finally {
			loading.value = false
		}
	}

	const previewImage = (index) => {
		uni.previewImage({
			urls: diaryImgs.value,
			current: index
		})
	}

	const editDiary = () => {
		if (diary.value) {
			uni.navigateTo({
				url: `/pages/write/index?id=${diary.value.id}`
			})
		}
	}

	const deleteDiary = () => {
		uni.showModal({
			title: '确认删除',
			content: '确定要删除这篇日记吗？',
			success: function(res) {
				if (res.confirm) {
					performDelete()
				}
			}
		})
	}

	const performDelete = async () => {
		try {
			const response = await request.post(`/diary/delete/${diary.value.id}`)

			if (response.code === 200) {
				uni.showToast({
					title: '删除成功',
					icon: 'success'
				})

				// 返回上一页
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			} else {
				uni.showToast({
					title: '删除失败',
					icon: 'none'
				})
			}
		} catch (error) {
			console.error('删除日记失败:', error)
			uni.showToast({
				title: '删除失败',
				icon: 'none'
			})
		}
	}

	// 生命周期
	onLoad((options) => {
		console.log("options++++++++", options);
		if (options.id) {
			loadDiaryDetail(options.id)
		} else {
			loading.value = false
			uni.showToast({
				title: '参数错误',
				icon: 'none'
			})
		}
	})
</script>

<style scoped lang="scss">
	.diary-detail-container {
		padding: 20rpx;
		background: $uni-color-brown;
		min-height: 100vh;
		font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
		box-sizing: border-box;
	}

	.diary-card {
		background: rgba(255, 255, 255, 0.9);
		border-radius: 20rpx;
		box-shadow: 0 8rpx 24rpx rgba(139, 69, 19, 0.2);
		overflow: hidden;
		margin-bottom: 20rpx;
	}

	.diary-header {
		padding: 30rpx;
		background: $uni-color-brown1;
		color: #8b4513;
	}

	.title-section {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.diary-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #8b4513;
		flex: 1;
	}

	.mood-tag {
		font-size: 40rpx;
		margin-left: 20rpx;
	}

	.date-section {
		display: flex;
		justify-content: space-between;
		font-size: 26rpx;
	}

	.diary-date {
		font-weight: 500;
	}

	.diary-time {
		opacity: 0.8;
	}

	.diary-content {
		padding: 30rpx;
		min-height: 300rpx;
		color: #8b4513;
		font-size: 30rpx;
		line-height: 1.6;
	}

	.normal-content {
		white-space: pre-wrap;
	}

	.markdown-content {
		white-space: normal;
	}

	.markdown-content h1 {
		font-size: 42rpx;
		font-weight: bold;
		margin: 20rpx 0;
	}

	.markdown-content h2 {
		font-size: 38rpx;
		font-weight: bold;
		margin: 18rpx 0;
	}

	.markdown-content h3 {
		font-size: 34rpx;
		font-weight: bold;
		margin: 16rpx 0;
	}

	.markdown-content p {
		margin: 16rpx 0;
		line-height: 1.6;
	}

	.markdown-content ul,
	.markdown-content ol {
		padding-left: 40rpx;
		margin: 16rpx 0;
	}

	.markdown-content li {
		margin-bottom: 10rpx;
	}

	.markdown-content code {
		background-color: #f0f0f0;
		padding: 4rpx 8rpx;
		border-radius: 4rpx;
		font-family: monospace;
	}

	.markdown-content pre {
		background-color: #f0f0f0;
		padding: 20rpx;
		border-radius: 10rpx;
		overflow: auto;
		margin: 20rpx 0;
	}

	.markdown-content blockquote {
		border-left: 6rpx solid #d2b48c;
		padding-left: 20rpx;
		margin: 20rpx 0;
		color: #666;
	}

	.image-section {
		padding: 0 30rpx 30rpx;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #8b4513;
		margin-bottom: 20rpx;
	}

	.image-list {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
	}

	.image-item {
		width: 180rpx;
		height: 180rpx;
		border-radius: 15rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 12rpx rgba(139, 69, 19, 0.1);
	}

	.diary-image {
		width: 100%;
		height: 100%;
	}

	.action-buttons {
		display: flex;
		padding: 30rpx;
		gap: 20rpx;
	}

	.action-btn {
		flex: 1;
		border: none;
		border-radius: 15rpx;
		padding: 20rpx;
		font-size: 30rpx;
		font-weight: bold;
		
		color: #8b4513;
		box-shadow: 0 4rpx 12rpx rgba(139, 69, 19, 0.2);
	}

	.edit-btn {
		background: $uni-color-brown1;
	}

	.delete-btn {
		background: linear-gradient(to right, #ff9a8b, #ff6b6b);
		color: white;
	}

	.loading-container,
	.empty-container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 500rpx;
		font-size: 32rpx;
		color: #8b4513;
		background: rgba(255, 255, 255, 0.7);
		border-radius: 20rpx;
		box-shadow: 0 8rpx 24rpx rgba(139, 69, 19, 0.2);
	}
</style>